#  Copyright (C) 2010 Luca Piccinelli
# 
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

'''
Created on Apr 30, 2011

@author: picci
'''

class Borg(object):
    '''
    This provides a superclass for the Borg design Pattern.
    Classes inheriting from this one, all share the same state
    '''

    _shared_state = {}
    def __new__(cls, *p, **k):
        '''
        Binds the __dict__ attribute of any inheriting class to the Borg shared one
        '''
        self = object.__new__(cls)
        self.__dict__ = cls._shared_state
        return self 
        
        
class Singleton(object):
    '''
    This provides a superclass for the Singleton design Pattern.
    Instantiating classes inheriting from this one, you are always granted to get the same
    instance.
    '''
    
    def __new__(cls, *p, **k):
        '''
        Returns the instance if it exists, or it creates one if not.
        '''
        if not "_instance" in cls.__dict__:
            cls._instance = object.__new__(cls)
        return cls._instance    
        